package chapter02
import scala.collection.mutable
object Test36_wordCount {
  def main(args: Array[String]): Unit = {
    // 单词计数：将集合中出现的相同的单词，进行计数，取计数排名前三的结果
    val stringList =
      List("Hello Scala Hbase kafka", "Hello Scala Hbase", "Hello Scala", "Hello")
    val map = mutable.Map[String,Int]()
    val list = stringList.map(e => {
      e.split(" ").toList
    })
    println(list)
    for(i<-list){
      for(j<-i){
        map.put(j,map.getOrElse(j,0)+1)
      }
    }
    println(map)
    //使用算子计算
    println(stringList.flatMap(e => e.split(" ").toList)
      .groupBy(e=>e)
    .map(e=>{e._1->e._2.size}))
    println(stringList.flatMap(e => e.split(" ").toList)
      .groupBy(e=>e)
      .mapValues(e=>e.size))
  }
}
